﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using Bot.Domain.Specifications;
using AutoMapper;

namespace Bot.Domain.Categories.Service
{
    public class IntentQuestionDomainService : IIntentQuestionDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, IntentQuestion> _repository;
        public IMapper Mapper { get; set; }

        public IntentQuestionDomainService(IRepository<Guid, IntentQuestion> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public IntentQuestion Create(IntentQuestion qustion)
        {
            if(qustion.Id == Guid.Empty)
            {
                qustion.Id = Guid.NewGuid();
            }

            qustion = _repository.Create(qustion);

            if(qustion.SelectedKeywords!=null&& qustion.SelectedKeywords.Count > 0)
            {
                IIntentQuestionSelectedKeywordDomainService intentQuestionSelectedKeywordDomainService = _container.Resolve<IIntentQuestionSelectedKeywordDomainService>();
                foreach(var keyword in qustion.SelectedKeywords)
                {
                    keyword.QuestionId = qustion.Id;
                    keyword.Id = Guid.NewGuid();
                    intentQuestionSelectedKeywordDomainService.Create(keyword);
                }
            }

            return qustion;
        }

        public IntentQuestion Get(Guid id, string include= "")
        {
            var intentQuestion = _repository.Get(id);
            if (intentQuestion != null && !string.IsNullOrEmpty(include) && include.Split(',').Contains("selectedKeywords"))
            {
                IIntentQuestionSelectedKeywordDomainService intentQuestionSelectedKeywordDomainService = _container.Resolve<IIntentQuestionSelectedKeywordDomainService>();
                intentQuestion.SelectedKeywords = intentQuestionSelectedKeywordDomainService.GetByQuestionId(id, include).ToList();
            }
            return intentQuestion;
        }

        public IntentQuestion Update(IntentQuestionUpdateBo bo)
        {
            IntentQuestion intentQuestion = _repository.Get(bo.Id);
            IIntentQuestionSelectedKeywordDomainService intentQuestionSelectedKeywordDomainService = _container.Resolve<IIntentQuestionSelectedKeywordDomainService>();
            var keywordIds = new List<Guid> { };
            if (bo.SelectedKeywords!=null&& bo.SelectedKeywords.Count > 0)
            {
                foreach (var keywordBo in bo.SelectedKeywords)
                {     
                    if (keywordBo.Id == Guid.Empty)
                    {
                        //新增
                        var keyword = Mapper.Map<IntentQuestionSelectedKeyword>(keywordBo);
                        keyword.Id = Guid.NewGuid();
                        keyword.QuestionId = bo.Id;
                        intentQuestionSelectedKeywordDomainService.Create(keyword);
                        keywordIds.Add(keyword.Id);
                    }
                    else
                    {
                        //修改
                        keywordBo.QuestionId = bo.Id;
                        intentQuestionSelectedKeywordDomainService.Update(keywordBo);
                        keywordIds.Add(keywordBo.Id);
                    }
                }
            }

            //删除
            var keywords = intentQuestionSelectedKeywordDomainService.GetByQuestionId(bo.Id);
            foreach (var keyword in keywords)
            {
                if (!keywordIds.Contains(keyword.Id))
                {
                    intentQuestionSelectedKeywordDomainService.Delete(keyword.Id);
                }
            }

            intentQuestion.Content = bo.Content;

            _repository.Update(intentQuestion);

            return intentQuestion;

        }

        public IEnumerable<IntentQuestion> List()
        {
            return _repository.ListAll();
        }

        public void Delete(Guid id)
        {
            IntentQuestion intentQuestion = Get(id, include: "selectedKeywords");
            if (intentQuestion!=null&& intentQuestion.SelectedKeywords!=null&& intentQuestion.SelectedKeywords.Count > 0)
            {
                IIntentQuestionSelectedKeywordDomainService intentQuestionSelectedKeywordDomainService = _container.Resolve<IIntentQuestionSelectedKeywordDomainService>();
                foreach (var keyword in intentQuestion.SelectedKeywords)
                {
                    intentQuestionSelectedKeywordDomainService.Delete(keyword.Id);
                }
            }
            
            _repository.Delete(intentQuestion);

        }

        public List<IntentQuestion> GetByIntentIds(List<Guid> intentIds)
        {
            return _repository.List(new IntentQuestionFilterSpecification(intentIds)).ToList();
        }
        public List<IntentQuestion> GetByIntentId(Guid intentId, string include = "")
        {
            var list = _repository.List(new IntentQuestionFilterSpecification(intentId)).ToList();

            if (list != null && list.Count > 0)
            {
                IIntentQuestionSelectedKeywordDomainService intentQuestionSelectedKeywordDomainService = _container.Resolve<IIntentQuestionSelectedKeywordDomainService>();
                foreach (var question in list)
                {
                    question.SelectedKeywords = intentQuestionSelectedKeywordDomainService.GetByQuestionId(question.Id, include).ToList();
                }
            }
            return list;
        }
        public List<IntentQuestion> GetByIds(Guid intentId, List<Guid> ids)
        {
            return _repository.List(new IntentQuestionFilterSpecification(intentId,ids)).ToList();
        }

        public List<IntentQuestion> BatchCreate(Guid intentId, List<IntentQuestion> intentQuestions)
        {
            var intentQuestionList = GetByIntentId(intentId);
            foreach (var question in intentQuestions)
            {
                if (!intentQuestionList.Exists(a => a.Content.Trim() == question.Content.Trim()))
                {//重复的question 屏蔽掉
                    intentQuestionList.Add(Create(question));
                }
            }
            return intentQuestionList;

        }
    }
}
